package tan.dft.gps;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import tan.dft.quanly.Qlbiquanly;
import tan.dft.quanly.QlbiquanlyAdapter;
import android.app.Activity;
import android.content.Intent;
import android.location.Address;
import android.location.Geocoder;
import android.os.Bundle;
import android.widget.ListView;

public class Danhsachbiquanly extends Activity {

	private String myjsonstring, add;
	private Geocoder geocoder;
	private int idmayintent, idbql1;
	private List<Address> addresses;
	private ListView listquanly;
	private ArrayList<Qlbiquanly> array;
	private QlbiquanlyAdapter adapter;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.danhsachquanly);

		Intent intent = getIntent();
		Bundle bundle = intent.getExtras();
		idmayintent = bundle.getInt("idmay");

		listquanly = (ListView) findViewById(R.id.listdanhsachquanly);
		array = new ArrayList<Qlbiquanly>();
		adapter = new QlbiquanlyAdapter(Danhsachbiquanly.this,
				R.layout.biquanlyitem, array);
		creatArray();
		listquanly.setAdapter(adapter);

	}

	private void creatArray() {
		StringBuffer sb = new StringBuffer();
		BufferedReader br = null;
		try {
			br = new BufferedReader(new InputStreamReader(getAssets().open(
					"jsondata.txt")));
			String temp;
			while ((temp = br.readLine()) != null)
				sb.append(temp);
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				br.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

		myjsonstring = sb.toString();
		try {
			JSONObject jsonObjMain = new JSONObject(myjsonstring);
			JSONArray jsonArray = jsonObjMain.getJSONArray("data");
			for (int i = 0; i < jsonArray.length(); i++) {
				JSONObject jsonObj = jsonArray.getJSONObject(i);
				int idmay = (Integer) jsonObj.getInt("idmay");
				if (idmay == idmayintent) {
					JSONArray jsonArraybql = jsonObj.getJSONArray("idbql");
					for (int k = 0; k < jsonArraybql.length(); k++) {
						JSONObject jsonObjectbql = jsonArraybql
								.getJSONObject(k);
						idbql1 = jsonObjectbql.getInt("idbql1");
						String name = jsonObjectbql.getString("name");
						String gender = jsonObjectbql.getString("gender");
						int age = jsonObjectbql.getInt("age");
						JSONObject jObj = jsonObjectbql.getJSONObject("toado");
						Double lat = jObj.getDouble("lat");
						Double lng = jObj.getDouble("lng");

						geocoder = new Geocoder(this, Locale.getDefault());
						try {
							addresses = geocoder.getFromLocation(lat, lng, 1);
							String address = addresses.get(0).getAddressLine(0);
							String city1 = addresses.get(0).getAddressLine(1);
							String country = addresses.get(0).getAddressLine(2);
							add = address + " | " + city1 + " | " + country;

						} catch (IOException e) {
							e.printStackTrace();
						}

						Qlbiquanly quanly = new Qlbiquanly(idmay, idbql1, age,
								name, add, gender, lat, lng);
						array.add(quanly);

					}

				}

			}

		} catch (JSONException e) {
			e.printStackTrace();
		}
	}
}
